<template>
  <div class="comment-item">
    <div class="comment-header">
      <span class="user-name">{{ comment.userName || '匿名用户' }}</span>
      <span class="comment-time">{{ formatTime(comment.createdAt) }}</span>
    </div>
    <div class="comment-content">{{ comment.content }}</div>
    <div class="comment-actions">
      <el-button type="text" size="small" @click="emitReply">回复</el-button>
    </div>
    <div v-if="comment.children && comment.children.length" class="comment-children">
      <CommentItem
        v-for="child in comment.children"
        :key="child.commentID"
        :comment="child"
        @reply="emitReply"
      />
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue'

const props = defineProps({
  comment: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['reply'])

const formatTime = (timeString) => {
  if (!timeString) return '';
  const date = new Date(timeString);
  return date.toLocaleString(); // 或根据需求格式化
};

const emitReply = () => {
  emit('reply', props.comment.commentID, props.comment.userName)
}
</script>

<style scoped>
.comment-item {
  border-bottom: 1px solid #EBEEF5;
  padding: 15px 0;
  margin-left: 0px; /* Base indentation */
}

.comment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
}

.user-name {
  font-weight: bold;
  color: #303133;
}

.comment-time {
  font-size: 12px;
  color: #909399;
}

.comment-content {
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
  margin-bottom: 10px;
}

.comment-actions {
  text-align: right;
}

.comment-children {
  margin-left: 20px; /* Indent children comments */
  border-left: 2px solid #EBEEF5;
  padding-left: 10px;
  margin-top: 10px;
}

/* For nested comments, add more indentation */
.comment-children .comment-item {
  padding-top: 10px;
  padding-bottom: 5px;
  border-bottom: none; /* No bottom border for nested items */
}
</style> 